package wgff.xml.updates
{
	import flash.desktop.Updater;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.*;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	[Event(name="isUpdated", type="flash.events.Event")]
	[Event(name="isUpToDate", type="flash.events.Event")]
	[Event(name="errorUpdate", type="flash.events.Event")]
	
	public class updates {
		
		private var updateFileUrl:String = "";
		private var currentVersion:Object = "";
		private var fileData:ByteArray = new ByteArray();
		private var urlStream:URLStream = new URLStream();
		private var myXML:XML = new XML();
		
		public function updates() {
			
		}
		
		public function checkForUpdate(url:String, version:Object):void {
			this.updateFileUrl = url;
			this.currentVersion = version;
			var http:HTTPService = new HTTPService();
			http.resultFormat = "text";
			http.method = "GET";
			http.url = url;
			http.addEventListener(ResultEvent.RESULT, httpResult);
			http.addEventListener(FaultEvent.FAULT, httpFault);
			http.send();
		}
		
		private function httpResult(event:ResultEvent):void {
			var result:Object = event.result;
			this.myXML = new XML(result);
			if (this.compareVersions(this.currentVersion, this.myXML)) {
				Alert.yesLabel = "Update";
				Alert.show("There is a new version of " + this.myXML.item.name + " available. Do you want to update this program?\n\nDescription:\n" + this.myXML.item.description, "Update?", Alert.YES | Alert.CANCEL, null, doUpdate, null, Alert.YES);
			}
		}
		
		private function doUpdate(event:CloseEvent):void {
			if (event.detail == Alert.YES) this.downloadUpdate(this.myXML.item.path);
		}
		
		private function compareVersions(curent:Object, available:XML):Boolean {
			if (curent["major"] < available.item.version.major) return true;
			if (curent["minor"] < available.item.version.minor) return true;
			if (curent["revision"] < available.item.version.revision) return true;
			if (curent["build"] < available.item.version.build) return true;
			return false;
		}

		private function httpFault(event:FaultEvent):void {
			var faultstring:String = event.fault.faultString;
			Alert.show(faultstring);
		}

		public function updateApplication():void {
			var updater:Updater = new Updater(); 
			var airFile:File = File.applicationStorageDirectory.resolvePath("update.air");
			var version:String = "2.01"; 
			updater.update(airFile, version);			
		}
		
		public function downloadUpdate(url:String):void {
			var urlReq:URLRequest = new URLRequest(url);
			this.urlStream.addEventListener(Event.COMPLETE, loaded);
			this.urlStream.load(urlReq);
		}
		
		private function loaded(event:Event):void { 
		    urlStream.readBytes(this.fileData, 0, this.urlStream.bytesAvailable); 
		    this.writeAirFile(); 
		} 
		 
		private function writeAirFile():void { 
		    var airFile:File = File.applicationStorageDirectory.resolvePath("update.air");
		    var fileStream:FileStream = new FileStream(); 
		    fileStream.open(airFile, FileMode.WRITE); 
		    fileStream.writeBytes(this.fileData, 0, this.fileData.length); 
		    fileStream.close(); 
		    this.updateApplication();
		}
	}
}